<template>
  <div>
    <!-- inline代表的行内表单 -->
    <el-form :inline="true" class="demo-form-inline" :model="cForm">
        <el-form-item label="一级分类">
            <el-select placeholder="请选择" v-model="cForm.category1Id" @change="handler1" :disabled="show">
                <el-option 
                    v-for="c1 in list1" 
                    :key="c1.id" 
                    :label="c1.name" 
                    :value="c1.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
            <el-select placeholder="请选择" v-model="cForm.category2Id" @change="handler2" :disabled="show">
                <el-option 
                    v-for="c2 in list2" 
                    :key="c2.id" 
                    :label="c2.name" 
                    :value="c2.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="三级分类">
            <el-select placeholder="请选择" v-model="cForm.category3Id" @change="handler3" :disabled="show">
                <el-option 
                    v-for="c3 in list3" 
                    :key="c3.id" 
                    :label="c3.name" 
                    :value="c3.id">
                </el-option>
            </el-select>
        </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
    name:'CategorySelect',
    props:['show'],
    data() {
        return {
            list1:[],
            list2:[],
            list3:[],
            // 收集相应的一级二级三级分类id
            cForm:{
                category1Id:'',
                category2Id:'',
                category3Id:'',
            }
        }
    },
    mounted(){
      this.getCategory1List()
    },
    methods:{
        // 获取一级分类数据的方法
        getCategory1List(){
            this.$API.attr.reqCategory1List().then(res=>{
                // console.log(res);
                if(res.code == 200){
                    this.list1 = res.data
                }
            })
        },
        // 一级分类select事件回调触发，获取二级分类数据的方法
        handler1(){
            // 清除数据
            this.list2 = []
            this.list3 = []
            this.cForm.category2Id=''
            this.cForm.category3Id=''
            this.$API.attr.reqCategory2List(this.cForm.category1Id).then(res=>{
                // console.log(res);
                if(res.code == 200){
                    this.list2 = res.data
                }
            })
        },
        // 二级分类select事件回调触发，获取三级分类数据
        handler2(){
            // 清除数据
            this.list3 = []
            this.cForm.category3Id=''
            this.$API.attr.reqCategory3List(this.cForm.category2Id).then(res=>{
                // console.log(res);
                if(res.code == 200){
                    this.list3 = res.data
                }
            })
        },
        // 三级分类的事件回调
        handler3(){
            this.$emit('getCategoryId',this.cForm.category1Id,this.cForm.category2Id,this.cForm.category3Id);
        }
    },
}
</script>

<style>

</style>